
package com.wa.nashorn;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

@Service
public class ScriptEngineServiceImpl implements ScriptEngineService {
    @Autowired
    private NashornEngineService nashornEngineService;

    public ScriptEngineServiceImpl() {
    }

    public void push(Script script) {
        Assert.notNull(script, "script should be not null");
        if (script instanceof NashornScript) {
            this.nashornEngineService.push(ContextNashornScript.wrapper((NashornScript)script));
        }

    }

    public <T> T poll(BlockingQueue<T> queue) {
        return this.poll(queue, 60);
    }

    public <T> T poll(BlockingQueue<T> queue, Integer timeoutSeconds) {
        try {
            Object result = queue.poll((long)timeoutSeconds, TimeUnit.SECONDS);
            if (result instanceof Exception) {
                throw new RuntimeException((Exception)result);
            } else {
                return (T) result;
            }
        } catch (InterruptedException var4) {
            throw new IllegalStateException("Illegal state or script engine", var4);
        }
    }
}
